/**
 * Created by zycool on 2016/9/6.
 * 涂层维修 - 维修整改 - 腐蚀控制
 */

;(function ($, window, document, undefined) {

    // 管道
    var $pipeline = $('#JQ_pipeline').select3();
    // 管段
    var $segment = $('#JQ_segment').select3({
        title: '管段'
    });
    // 起点
    var $begin = $('#JQ_begin').select3({
        title: '起点位置',
        server: true,
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment}
    });
    // 终点
    var $end = $('#JQ_end').select3({
        title: '终点位置',
        server: true,
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment, 'beginSignId': $begin}
    });
    // 触发管道
    $pipeline.element.on('select', function (e, v) {
        var id = v || $pipeline.val();
        if (id) {
            // 管段
            DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: id}, function (dataset) {
                var json = DyyBase.getSelect(dataset);
                $segment.add(json);
            });
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin');
                var end = DyyBase.getPos(dataset, 'end');
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $segment.add([]);
            $begin.add([]);
            $end.add([]);
        }
    });
    // 触发管段
    $segment.element.on('select', function (e, v) {
        var id = v || $segment.val();
        if (id) {
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin');
                var end = DyyBase.getPos(dataset, 'end');
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $begin.add([]);
            $end.add([]);
        }
    });
    // 任务状态
    var $status = $('#JQ_status').select3({
        search: false,
        data: [
            {id: 'state01', text: '待发布'},
            {id: 'WAIT_FOR_REPAIR', text: '待维修'},
            {id: 'MAINTENANCE', text: '进行中'},
            {id: 'ALREADY_REPAIR', text: '已维修'}
        ]
    });
    // 开工时间 1
    var $date_01 = $('#JQ_input_date_01');
    // 开工时间 2
    var $date_02 = $('#JQ_input_date_02');
    // 表格
    var $repair_table = $('#JQ_repair_table').checkbox3();

    // 筛选对象
    var filter = {
        // 搜索
        getSearch: function (param) {
            var data = {};
            data.mode = true;
            // 分页参数
            if (param) {
                if (param.start == 0) {
                    data['jsonBean.pageNum'] = 1;
                } else {
                    data['jsonBean.pageNum'] = param.start / 10 + 1;
                }
                data['jsonBean.pageCount'] = param.length;
            }
            data.pipeLineId = function () {
                var v = $pipeline.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.pipeLineName = function () {
                var v = $pipeline.val('text');
                if (v) {
                    return v;
                }
                return '';
            }();
            data.pipeSegmentId = function () {
                var v = $segment.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.beginMileage = function () {
                var v = $begin.val('mile');
                if (v) {
                    return v;
                }
                return '';
            }();
            data.endMileage = function () {
                var v = $end.val('mile');
                if (v) {
                    return v;
                }
                return '';
            }();
            data.dataAndRepairState = function () {
                var v = $status.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.planStartDate1 = function () {
                var v = $date_01.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.planStartDate2 = function () {
                var v1 = $date_01.val();
                var v2 = $date_02.val();
                if (v2) {
                    if (v1) {
                        if (!(Dyy.checkDateLate(v1, v2))) {
                            $date_02.TIP({content: '结束时间须晚于开始时间!'});
                            data.mode = false;
                        }
                    }
                    return v2;
                }
                return '';
            }();

            return data
        },
        // 选中任务
        getSelectIds: function () {
            var $chk = $repair_table.find('i.zy-checkbox');
            var arr = [];
            if ($chk.length > 0) {
                $chk.each(function () {
                    var $this = $(this);
                    if ($this.hasClass('active')) {
                        arr.push({name: 'repairTaskId', value: $this.attr('data-id')});
                    }
                });
            }
            return arr;
        },
        // 导出
        getExport: function () {
            return filter.getSearch();
        }
    };

    // 加载表格
    var DT01 = $repair_table.DataTable({
        'language': {
            'paginate': {
                'next': '下一页',
                'previous': '上一页'
            },
            'info': '本次检索共 _TOTAL_ 条，每页显示 10 条，共 _PAGES_ 页',
            'zeroRecords': DyyBase.table_empty(),
            'infoEmpty': ''
        },
        'autoWidth': false,
        'order': [],
        'serverSide': true,
        'pageLength': 10,
        'ordering': false,
        'processing': false,
        'searching': false,
        'lengthChange': false,
        'ajax': function (data, callback) {
            var param = filter.getSearch(data);
            DyyBase.ajax(corrctrlApi.url_paint_list(), param, function (dataset) {
                var returnData = {};
                returnData.draw = data.draw;
                returnData.recordsTotal = dataset.result.total;
                returnData.recordsFiltered = dataset.result.total;
                returnData.data = DyyBase.clearNull(dataset.result.list);
                callback(returnData);
            });
        },
        'drawCallback': function () {
            $('i.zy-checkbox-all').trigger('clear');
        },
        'columns': [
            {
                data: 'repairTaskId', render: function (data) {
                return '<i class="zy-checkbox" data-id="' + data + '" title="选中"></i>';
            }
            },
            {
                data: 'name', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'pipeLineName', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'fromPointName', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'toPointName', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'planStartDate', render: function (data) {
                var time = Dyy.getDate(data);
                return '<span title="' + time + '">' + time + '</span>';
            }
            },
            {
                data: 'planCompletionDate', render: function (data) {
                var time = Dyy.getDate(data);
                return '<span title="' + time + '">' + time + '</span>';
            }
            },
            {
                data: 'dataState_label', render: function (data) {
                var color = '';
                switch (true) {
                    case (data == '待发布' || data == '已维修'):
                        color = 'zy-status-gray';
                        break;
                    case (data == '难以维修'):
                        color = 'zy-status-red';
                        break;
                    case (data == '待维修' || data == '进行中'):
                        color = 'zy-status-blue';
                        break;
                    default:
                        break;
                }
                return '<span title="' + data + '" class="' + color + '">' + data + '</span>';
            }
            },
            {
                data: 'repairTaskId', render: function (data, type, row) {
                if (row.dataState == 'state20') {
                    return '<i title="编辑" class="zy-record-add-icon disabled" data-id="' + data + '"></i>';
                } else {
                    return '<a href="' + DyyBase.root_path + '/corrctrl/CoatRepairTask/initCreate.html?repairTaskId=' + data + '"><i title="编辑" class="zy-record-add-icon" data-id="' + data + '"></i></a>';
                }
            }
            },
            {
                data: 'repairTaskId', render: function (data) {
                return '<a href="' + DyyBase.root_path + '/corrctrl/CoatRepairTask/initDetail.html?repairTaskId=' + data + '"><i title="查看详情" class="zy-details-icon" data-id="' + data + '"></i></a>';
            }
            },
            {
                data: 'repairTaskId', render: function (data) {
                return '<a href="' + DyyBase.root_path + '/corrctrl/repairMgr/toStatistics.html?repairTaskId=' + data + '"><i title="统计" class="zy-record-tj-icon" data-id="' + data + '"></i></a>';
            }
            }
        ]
    });


    // 搜索
    $('#JQ_search_repair').click(function () {
        var param = filter.getSearch();
        if (param.mode) {
            if (DT01) {
                DT01.draw();
            }
        }
    });

    // 删除
    $('#JQ_delete').click(function () {
        var $this = $(this);
        var param = filter.getSelectIds();
        if (param.length) {
            $this.MSG({
                type: 'confirm',
                tag: 'no',
                height: 230,
                content: '请确认是否删除所选的维修项目？',
                yesBtnTxt: '确认',
                yesBtnAct: function () {
                    DyyBase.ajax(corrctrlApi.url_paint_delete(), param, function (dataset) {
                        if (dataset.bool) {
                            $this.MSG({
                                type: 'success',
                                content: '已选择删除 ' + dataset.result.count + ' 条维修项目,<br>成功删除 ' + dataset.result.successCount + ' 条。',
                                yesBtnAct: function(){
                                    DT01.draw();
                                },
                                closeBtnAct: function(){
                                    DT01.draw();
                                }
                            });
                        } else {
                            $this.MSG({
                                type: 'error',
                                content: dataset.result
                            });
                        }
                    });
                }
            });
        } else {
            $this.MSG({
                content: '请选择要删除的维修项目!'
            });
        }
    });

    // 关闭
    $('#JQ_close').click(function () {
        var $this = $(this);
        var param = filter.getSelectIds();
        if (param.length > 0) {
            DyyBase.ajax(corrctrlApi.url_paint_close(), param, function (dataset) {
                if (dataset.bool) {
                    $this.MSG({
                        type: 'success',
                        content: '关闭维修任务成功!',
                        yesBtnAct: function(){
                            DT01.draw();
                        },
                        closeBtnAct: function(){
                            DT01.draw();
                        }
                    });
                } else {
                    $this.MSG({
                        type: 'error',
                        content: dataset.result
                    });
                }
            });
        } else {
            $this.MSG({
                content: '请选择要关闭的维修任务!'
            });
        }
    });

    // 发布
    $('#JQ_release').click(function () {
        var $this = $(this);
        var param = filter.getSelectIds();
        if (param.length > 0) {
            DyyBase.ajax(corrctrlApi.url_paint_release(), param, function (dataset) {
                if (dataset.bool) {
                    $this.MSG({
                        type: 'success',
                        content: '已选择 ' + dataset.result.count + ' 条维修项目,<br>成功发布 ' + dataset.result.successCount + ' 条。',
                        yesBtnAct: function(){
                            DT01.draw();
                        },
                        closeBtnAct: function(){
                            DT01.draw();
                        }
                    });
                } else {
                    $this.MSG({
                        type: 'error',
                        content: dataset.result
                    });
                }
            });
        } else {
            $this.MSG({
                content: '请选择要发布的维修任务!'
            });
        }
    });

    // 导出
    $('#JQ_export').click(function () {
        var param = filter.getExport;
        if (param instanceof Function) {
            param = param();
        }
        $(this).EXPT({
            type: 'name',
            name: param.pipeLineName + '涂层维修项目列表',
            param: param,
            server: DyyBase.root_path + '/corrctrl/CoatRepairTask/toExcel.json'
        });
    });

    /*// 涂层维修详情模板
     var repairTemplate = [
     {name: 'name', title: '项目名称：'},
     {name: 'pipeLineName', title: '管道名称：'},
     {name: 'fromPointName', title: '起点位置：'},
     {name: 'toPointName', title: '终点位置：'},
     {
     name: 'planStartDate', title: '开工时间：', render: function (d) {
     return Dyy.getDate(d);
     }
     },
     {
     name: 'planCompletionDate', title: '竣工时间：', render: function (d) {
     return Dyy.getDate(d);
     }
     },
     {name: 'dataState_label', title: '任务状态：'},
     {name: 'leakagePointWorkLoad', title: '发布人：'},
     {
     name: 'createDate', title: '发布时间：', render: function (d) {
     return Dyy.getDate(d);
     }
     }
     ];

     // 展开详情
     $repair_table.on('click', 'i.zy-details-icon', function () {
     DyyBase.showDetails($(this), DT01, $repair_table, repairTemplate);
     });*/

})(jQuery, window, document);